/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gmail.avereshchaga.models;

import com.gmail.avereshchaga.transport.Client;
import com.gmail.avereshchaga.transport.entity.Item;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
public class ItemsListTableModel extends AbstractTableModel {

    private String columnNames[] = {
        "Name",
        "Code",
        "Item created at",
        "Item created by",
        "Price"};
    private ArrayList<Item> items =
            (ArrayList<Item>) Client.getItemsList();

    @Override
    public int getRowCount() {
        return items.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item recordFromItemsList = items.get(rowIndex);

        switch (columnIndex) {
            case 0:
                return recordFromItemsList.getName();
            case 1:
                return recordFromItemsList.getCode();
            case 2:
                DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
                String dateStr = dateFormat.format(recordFromItemsList.getCreatedAt());
                return dateStr;
            case 3:
                return recordFromItemsList.getUser().getFirstName() + " "
                        + recordFromItemsList.getUser().getLastName();
            case 4:
                return recordFromItemsList.getPrice();
            default:
                throw new UnsupportedOperationException("Not Supported Operation");
        }
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
}
